/**
 * 
 */
package com.johnny.xia.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.johnny.xia.util.file.AppendToFile;
import com.johnny.xia.util.file.ReadFromFile;

/**
 * @author johnnyxia
 *
 */
public class CMSUtil{
    //TDK 部分
    static String title="test title";
    
    static String description="test description";
    
    static String keyword="test keyword";

    // 国双代码监控部分
    static String pageType = "hahahah";

    static String pageName = "Campaign Page|GiftGuide-Mens";

    //内容部分
    static String contentDivId = "container";
    
    //html文件路径
    static String htmlPath = "D:\\baozun\\UA\\ua-official-store\\ua-huodong\\GiftGuideWomen-cn.html";
    
    //图片替换部分
    static String imgReplaceTag = "images/GiftGuide";
    
    static String imgReplacePath="http://staging.underarmour.cn/resources/images/test/GiftGuide";
    
    
    // ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼  不需要修改的部分  ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
    
    static String jsStart = "<script type=\"text/javascript\">";
    
    static String jsEnd = "</script>";
    
    static String cssStart = "<style type=\"text/css\">";
    
    static String cssEnd = "</style>";

    public static void main(String[] args){

        StringBuffer buffer = new StringBuffer();

        String html = ReadFromFile.readFileByLines(htmlPath);
        Document doc = Jsoup.parse(html);

        Element content = doc.getElementById(contentDivId);

        String css = ReadFromFile.readFileByLines(htmlPath, cssStart, cssEnd);
        
        if(isNotNullOrEmpty(css)){
            buffer.append(css.toString());
            buffer.append("\r\n");
        }
       
        String script = ReadFromFile.readFileByLines(htmlPath, jsStart, jsEnd);
        
        if(isNotNullOrEmpty(script)){
            buffer.append(script.toString());
            
            buffer.append("\r\n"); 
        }
        
        buffer.append(createTDK());
        buffer.append(createOther());
        
        buffer.append(content.toString().replaceAll(imgReplaceTag, imgReplacePath));

        System.out.println(buffer.toString());

    }

    private static String createOther(){
        StringBuffer buffer = new StringBuffer();

        buffer.append("<a href=\"#0\" class=\"cd-top\"></a>");
        buffer.append("\r\n");
        if (isNotNullOrEmpty(pageType)){
            buffer.append("<div id=\"pageType\" style=\"display:none\">" + pageType + "</div>")
                .append("\r\n");
        }

        if (isNotNullOrEmpty(pageName)){
            buffer.append("<div id=\"pageName\" style=\"display:none\">" + pageName + "</div>")
                .append("\r\n");
        }

        return buffer.toString();
    }
    
    
    private static String createTDK(){
        StringBuffer buffer = new StringBuffer();
        
        if(isNotNullOrEmpty(title)){
            buffer.append("<title>"+title+"</title> ")
                .append("\r\n");
        }
        if(isNotNullOrEmpty(keyword)){
            buffer.append("<meta name=\"keywords\" content=\""+keyword+"\" /> ")
                .append("\r\n");
        }
        
        if(isNotNullOrEmpty(description)){
            buffer.append("<meta name=\"description\" content=\""+description+"\"/>")
                .append("\r\n");
        }
        
        return buffer.toString();
    }

    private static boolean isNotNullOrEmpty(String str){
        if (null == str || str.trim().length() < 1){
            return false;
        }
        return true;
    }

}
